﻿using B.S.DataBase.MyProject.Domain.Infrastructures;
using B.S.DataBase.MyProject.Domain.SmartHealthcareZSJ;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.CarouselimageCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.CarouselimageCommandHandler
{
    /// <summary>
    /// 轮播图更新命令处理类
    /// </summary>
    public class CarouselimageUpdateCommandHandler:IRequestHandler<CarouselimageUpdateCommand,APIResult<object>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sugar"></param>
        public CarouselimageUpdateCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }

        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(CarouselimageUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                var model = await sugar.db.Queryable<Carouselimage>().Where(x => x.CarouseId== request.CarouseId).FirstAsync();
                model.CarouseId = request.CarouseId;
                model.CarouselTitle = request.CarouselTitle;
                model.Thumbnail = request.Thumbnail;
                model.Status = request.Status;
                model.CarouselLinkAddress = request.CarouselLinkAddress;
                model.Sorting = request.Sorting;


                var r = await sugar.db.Updateable(model).ExecuteCommandAsync();
                if (r > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }


            }
            catch (Exception ex)
            {

                // 记录异常信息
                Console.WriteLine($"修改设备管理数据时发生异常: {ex.Message}");
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);

        }
    }
}
